package cn.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import cn.hr.pojo.Organization;
import cn.hr.pojo.SysUser;
import cn.hr.utils.Constants;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.hr.service.OrganizationService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组织机构控制器
 * @author MO
 *
 */
@Controller
@RequestMapping("/sys/organization")
public class OrganizationController {
	private Logger logger = LoggerFactory.getLogger(OrganizationController.class);
	@Resource
	private OrganizationService organizationService;
	private String meagess;

	@RequestMapping("/get")
	public String get(@RequestParam("id")String id,Model model){
		try {
			Organization organ = organizationService.get(Integer.parseInt(id));
			model.addAttribute("organ",organ);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "organization/organizationSave";
	}

	/**
	 * 跳转到添加页面
	 * @return
	 */
	@RequestMapping("/add")
	public String add(){
		return "organization/organizationAdd";
	}

	/**
	 * 添加
	 * @param organ
	 * @param session
	 * @return
	 */
	@RequestMapping("/addSave")
	public String addSave(Organization organ, HttpSession session) {
		organ.setCreatedById(((SysUser) (session.getAttribute(Constants.USER_SESSION))).getId());
		organ.setCreatedDate(new Date());
		if(organ.getIsStart()==null){
			organ.setIsStart(0);
		}else{
			organ.setIsStart(1);
		}
		try {
			if (organizationService.insert(organ)){
				meagess = "redirect:list";
			}else{
				meagess = "redirect:add";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return meagess;
	}

	/**
	 * 跳转到修改页面
	 * @param _id
	 * @param model
	 * @return
	 */
	@RequestMapping("/update")
	public String update(@RequestParam(value="id")String _id,Model model){
		Organization organization = null;
		try {
			organization = organizationService.get(Integer.parseInt(_id));
			model.addAttribute("organ",organization);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  "organization/organizationUpdate";
	}

	/**
	 * 添加
	 * @param organ
	 * @param session
	 * @return
	 */
	@RequestMapping("/updateSave")
	public String updateSave(Organization organ,HttpSession session){
		organ.setModifyById(((SysUser) (session.getAttribute(Constants.USER_SESSION))).getId());
		organ.setModifyDate(new Date());
		if (organ.getIsStart()==null){
			organ.setIsStart(0);
		}else{
			organ.setIsStart(1);
		}
		try {
			if(organizationService.update(organ)){
				meagess = "redirect:list";
			}else{
				meagess = "redirect:update?id="+organ.getId();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return meagess;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public String delete(@RequestParam(value = "id")String id){
		try {
			meagess = organizationService.deleteById(Integer.parseInt(id));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  meagess;
	}

	/**
	 * 查询所有
	 * @return
	 */
	@RequestMapping("/organ")
	@ResponseBody
	public Object organList(){
		List<Map> list = null;
		Map map = new HashMap();
		map.put("parentId", 0);
		map.put("isStart", 1);
		try {
			list = organizationService.findSubdivisionByParentId(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSON.toJSON(list);
	}

	/**
	 * 组织页面
	 * @return
	 */
	@RequestMapping("/showList")
	public String list(@RequestParam(value = "fullName",required = false)String fullName,
					   @RequestParam(value = "is_start",required = false)String isStart,
					   @RequestParam(value = "shortName",required = false)String shortName,
					   @RequestParam(value="pageIndex",required = false)String _pageIndex,
					   Model model){
		Map<String,Object> map = new HashMap<>();
		if (fullName!=null&&fullName!="")map.put("fullName",fullName);model.addAttribute("fullName",fullName);
		if (isStart!=null&&isStart!="")map.put("isStart",isStart);model.addAttribute("isStart",isStart);
		if (shortName!=null&&shortName!="")map.put("shortName",shortName);model.addAttribute("shortName",shortName);
		Integer index = 1;
		if (_pageIndex!=null&&_pageIndex!=""){
			index = Integer.parseInt(_pageIndex);
		}
		try {
			PageInfo<Map> pageInfo = organizationService.findListByCondition(map,index);
			model.addAttribute("pageInfo",pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "organization/showList";
	}

	@RequestMapping("/list")
	public String showList(){
		return "organization/organizationList";
	}

}
